<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>标记分割字符串</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.strtok" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">strtok</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">strtok</span> &mdash; <span class="dc-title">标记分割字符串</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.strtok-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>strtok</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$token</code></span>
   ) : <span class="type">string</span></div>

  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>strtok</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$token</code></span>
   ) : <span class="type">string</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>strtok()</strong></span> 将字符串 <code class="parameter">str</code> 分割为若干子字符串，每个子字符串以 <code class="parameter">token</code> 中的字符分割。这也就意味着，如果有个字符串是 &quot;This is an example string&quot;，你可以使用空格字符将这句话分割成独立的单词。
  </p>
  <p class="para">
   注意仅第一次调用 strtok 函数时使用 string 参数。后来每次调用 strtok，都将只使用 token 参数，因为它会记住它在字符串 string 中的位置。如果要重新开始分割一个新的字符串，你需要再次使用 string 来调用 strtok 函数，以便完成初始化工作。注意可以在 token 参数中使用多个字符。字符串将被该参数中任何一个字符分割。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.strtok-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">str</code></dt>

     <dd>

      <p class="para">
       被分成若干子字符串的原始<span class="type"><span class="type 字符串">字符串</span></span>。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">token</code></dt>

     <dd>

      <p class="para">
       分割 <code class="parameter">str</code> 时使用的分界字符。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.strtok-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   标记后的<span class="type"><span class="type 字符串">字符串</span></span>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.strtok-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5984">
    <p><strong>Example #1 <span class="function"><strong>strtok()</strong></span> 范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"This&nbsp;is\tan&nbsp;example\nstring"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/*&nbsp;使用制表符和换行符作为分界符&nbsp;*/<br /></span><span style="color: #0000BB">$tok&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"&nbsp;\n\t"</span><span style="color: #007700">);<br /><br />while&nbsp;(</span><span style="color: #0000BB">$tok&nbsp;</span><span style="color: #007700">!==&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Word=</span><span style="color: #0000BB">$tok</span><span style="color: #DD0000">&lt;br&nbsp;/&gt;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tok&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #DD0000">"&nbsp;\n\t"</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5985">
    <p><strong>Example #2 当存在空的部分时 <span class="function"><strong>strtok()</strong></span> 的反应</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$first_token&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #DD0000">'/something'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'/'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$second_token&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #DD0000">'/'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$first_token</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$second_token</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
    string(9) &quot;something&quot;
    bool(false)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.strtok-notes">
  <h3 class="title">注释</h3>
  
  <div class="warning"><strong class="warning">Warning</strong><p class="simpara">此函数可能返回布尔值
<strong><code>FALSE</code></strong>，但也可能返回等同于 <strong><code>FALSE</code></strong> 的非布尔值。请阅读 <a href="language.types.boolean.html" class="link">布尔类型</a>章节以获取更多信息。应使用
<a href="language.operators.comparison.html" class="link">===
运算符</a>来测试此函数的返回值。</p></div>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.strtok-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="split.html" class="function" rel="rdfs-seeAlso">split()</a> - 用正则表达式将字符串分割到数组中</span></li>
    <li class="member"><span class="function"><a href="explode.html" class="function" rel="rdfs-seeAlso">explode()</a> - 使用一个字符串分割另一个字符串</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>